<?php
class profileController extends Zend_Controller_Action {
	
	public function indexAction() {
		$userid = $this->_helper->Acl->getUserId();
		$profile =Core_Business_User::getById($userid);
		$this->view->profile = $profile;
	}
	
	public function editAction(){
		if($this->getRequest()->isPost()) {
			$action = $this->getRequest()->getPost('changeCommonprofile');
			
			if (!isset($action)) {
				$action = $this->getRequest()->getPost('changePostalInfo');
				$action = $action == "change" ? "changePostalInfo" : $action;
			}
			else 
				$action = "editcommoprofile";
			
				
			$user =$this->_helper->Acl->getUser();
				switch ($action) {
					case "editcommoprofile":
						$user->Firstname = $this->getRequest()->getPost('firstname');
						$user->Lastname = $this->getRequest()->getPost('lastname');			
						break;
					case "changePostalInfo":
						$user->Zipcode = $this->getRequest()->getPost('zipcode');
						$user->City = $this->getRequest()->getPost('city');
						break;
				}	
			$user->save();
			$this->_redirect('/default/profile/index');
		}
	}
	
	public function changepwdAction() {
		
	}
	/**
	 * 
	 * Action used for altering the password
	 */
	public function changedpwdAction() {
		if($this->getRequest()->isPost()) {
			$oldpwd = $this->getRequest()->getPost('old');
			$newpwd = $this->getRequest()->getPost('newpwd');
			$confirmpwd = $this->getRequest()->getPost('confirm');
			$user =$this->_helper->Acl->getUser();
			if($user->checkPwd($oldpwd) && $newpwd==$confirmpwd) {
				$user->Password=$newpwd;
				$user->save();
				$this->view->Msg="Password has been changed";
			}
			else {
				$this->view->Msg="Password didn't changed";
			}
		}
	}
}